class_name UnitFlyMagic
extends UnitBase


var cooldown_timer: float = 0
@onready var ice_shoot_magic_marker: Marker2D = $ViewRoot/IceShootMagicMarker
const PARTICLE_ICE_MAGIC = preload("uid://cevb5m3rgdl4d")


func pick_new_wander_target():
	# 在矩形范围内随机目标点
	var x_offset = randf_range(-wander_width/2, wander_width/2)
	var y_offset = randf_range(-wander_height/2, wander_height/2)
	target_pos = home_position + Vector2(x_offset, y_offset)
	
	# 下一次切换目标时间
	timer = randf_range(1.0, change_dir_time)
	
	
func play_wander_animation():
	animation_player.play("idle")


func process_enter_magic(delta: float):
	if not attack_target:
		return
		
	if is_attack_target_out_of_senserange(): 
		return
	
	var config = unit_data.config 
	cooldown_timer -= delta
	if cooldown_timer <= 0:
		cooldown_timer = config.main_attack_cooldown
		states.transition("Magic")


func shoot_magic():
	if not attack_target: return
	
	var magic = PARTICLE_ICE_MAGIC.instantiate() as ParticleIceMagic
	
	var config = unit_data.config
	magic.attack = config.main_attack
	magic.critical_rate = config.main_critical_attack_rate
	magic.critical_bonus = config.main_critical_attack_bonus
	magic.knockback = config.main_attack_knockback
	magic.sender = self
	magic.level = level
	magic.hit_cam_shake_intensity = config.main_attack_cam_shake_intensity 
	magic.hit_cam_shake_duration = config.main_attack_cam_shake_duration 
	
	level.particles_parent.add_child(magic)
	magic.global_position = attack_target.global_position
	magic.set_is_enemy(true)
